var emailCount = 0;
var pageSize = 10;

function emailList(page, StartTime, EndTime) {
    $.ajax({
        url: "/PersonalCenter/GetEmailList",
        type: "post",
        data: { page: page, rows: pageSize, StartTime: StartTime, EndTime: EndTime },
        dataType: "json",
        async : false,
        success: function (list) {
            emailCount = list.total;
            $('#dg_emailList').html("<tr><th>标题</th><th>收件人</th><th>状态</th><th>留言时间</th><th>操作</th></tr>");
            if (list.total > 0) {
                $.each(list.rows, function (i, n) {
                    $('#dg_emailList').append("<tr style=\"" + trCss(n["status"]) + "\"><td>" + n["title"] + "</td>" +
                    "<td>" + n["tousername"] + "</td>" +
                    "<td>" + EmailStates(n["status"]) + "</td>" +
                    "<td>" + Datetime(n["createtime"]) + "</td>" +
                    "<td><a href=\"/PersonalCenter/EmailReply?id=" + n["id"] + "\">查看</a></td></tr>");
                });
            }
            else {
                $('#dg_emailList').append("<tr><td colspan=\"5\">暂时没有数据...</td></tr>");
            }
        }
    });
}

emailList(1, "", "");
//分页
$("#emailPage").createPage({
    pageCount: emailCount,
    pageSize: pageSize,
    current: 1,
    backFn: function (p) {
        emailList(p, $('#StartTime').datebox('getValue'), $('#EndTime').datebox('getValue'));
    }
});
function trCss(state)
{
    var res = "";
    if (state == '1')
    {
        res = "background-color: red;";
    }
    return res;
}

$("#btnEMSearch").click(function () {
    emailList(1, $('#StartTime').datebox('getValue'), $('#EndTime').datebox('getValue'));
});

function EmailStates(value) {
    var res = "";
    if (value == '0') {
        res = "未读";
    }
    else if (value == '1') {
        res = "已读";
    }
    else if (value == '2') {
        res = "已发送";
    }
    return res;
}